<?php

namespace Admin\Controller;

use Think\Controller;

class AuthController extends Controller
{
    public function login()
    {
        if (is_login(C('auth.admin'))) {
            $this->success('已登录，无需重复登录', U('/Admin/Index/index'));
        } else {
            if (IS_POST) {
                $postData = I('post.');
                $user = M('user')
                    ->where(array('name' => $postData['uname'], 'password' => md5($postData['upwd'])))
                    ->find();
                if ($user != NULL) {
                    //登录成功
                    session(C('auth.admin'), array('uid' => $user['id'], 'uname' => $user['uname']));
                    $this->success('登录成功', U('/Admin/Index/index'));
                } else {
                    $this->error('用户名或密码错误');
                }
            } else {
                return $this->display();
            }
        }
    }

    public function logout()
    {
        session(C('auth.admin'), NULL);
        return redirect(U('/Admin/Auth/login'));
    }
}